home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8206 / 8206.xpi / content / comm.js < prev    next >
Text File  |  2010-02-02  |  9KB  |  332 lines

  1. var WiseStampComm = function() {
  2.     var pub = {};
  3.  
  4.     pub.RESULT_OK = 0;
  5.     pub.RESULT_FAILURE = 1;
  6.     pub.RESULT_SERVER_DOWN = 2;
  7.     
  8.     //pub.HOST = "http://local.wisestamp";
  9.     pub.HOST = "http://my.wisestamp.com";
  10.  
  11.     pub.LOG_EVENT_INIT            = "addon_init";
  12.     pub.LOG_EVENT_SETTINGS        = "addon_settings";
  13.     pub.LOG_EVENT_SIG_INSERT    = "addon_sig_insert";
  14.     //pub.LOG_EVENT_INIT    = "addon_init";
  15.         
  16.     var URL_WRITE_LOG        = "/xml_api/write_log";
  17.     var URL_LOGIN            = "/xml_api/login";
  18.     var URL_REGISTER        = "/xml_api/register";
  19.     var URL_SAVE_SIGNATURES    = "/xml_api/save_signatures";
  20.     var URL_LOAD_SIGNATURE    = "/xml_api/load_signature";
  21.     var URL_LOAD_SIGNATURES    = "/xml_api/load_signatures";
  22.     var URL_LOAD_SETTINGS    = "/xml_api/load_settings";
  23.     
  24.     pub.xmlHttpObjects = new Array();
  25.     pub.mSettings = null; 
  26.  
  27.     ///////////////////////////////////////////////////////////////////////////
  28.  
  29.     pub.init = function()
  30.     {
  31.         var server_hostname = WiseStampPrefs.getCharPref("server");
  32.         
  33.         if (server_hostname != "")
  34.             pub.HOST = server_hostname; 
  35.         
  36.         WiseStampUtils.log("comm.js :: init :: server hostname = " + pub.HOST);
  37.     }
  38.     
  39.     function xmlToObject(xml)
  40.     {
  41.         var object = {};
  42.  
  43.         //WiseStampUtils.log("[comm.js::xmlToObject] HasChildNodes = " + xml.hasChildNodes());         
  44.         
  45.         object.nodeCount = xml.childNodes.length;
  46.             
  47.         if (xml.hasChildNodes()) 
  48.         {
  49.             //WiseStampUtils.log("[comm.js::xmlToObject] childNodes.length = " + xml.childNodes.length);
  50.             
  51.             for (var i = 0; i < xml.childNodes.length; i++)
  52.             {
  53.                 var child = xml.childNodes[i];
  54.                 
  55.                 //WiseStampUtils.log("[comm.js::xmlToObject] node " + i + " :: type = " + child.nodeType + ", tag = " + child.tagName + ", value = <" + child.nodeValue + ">");
  56.                 
  57.                 switch (child.nodeType)
  58.                 {
  59.                     case 1: //     Element Node
  60.                         if (object.nodes == undefined)
  61.                             object.nodes = {};
  62.                             
  63.                         object.nodes[child.tagName] = xmlToObject(child);
  64.                         break;
  65.                         
  66.                     case 2: //     Attribute Node
  67.                         if (object.attributes == undefined)
  68.                             object.attributes = {};
  69.                         
  70.                         object.attributes[child.tagName] = child.nodeValue;
  71.                         break;
  72.                     
  73.                     case 3: //     Text Node
  74.                     case 4: //     CDATA Section Node
  75.                         object.text = child.nodeValue;
  76.                         break;
  77.                     
  78.                     /*
  79.                     case 5: //     Entity Reference Node
  80.                     case 6: //     Entity Node
  81.                     case 7: //     Processing Instruction Node
  82.                     case 8: //     Comment Node
  83.                     case 9: //     Document Node
  84.                     case 10: //    Document Type Node
  85.                     case 11: //    Document Fragment Node
  86.                     case 12: //    Notation Node
  87.                     */
  88.                 }
  89.             }
  90.         }
  91.         
  92.         //WiseStampUtils.log("[comm.js::xmlToObject] <<<<");         
  93.         return object;
  94.     }
  95.     
  96.     function serializeParams(params)
  97.     {
  98.         var str = "";
  99.         
  100.         for (key in params) 
  101.         {
  102.             if (str.length > 0)
  103.                 str += "&";
  104.                 
  105.             str += key + '=' + params[key];
  106.         }
  107.         
  108.         return str;
  109.     }
  110.  
  111.     ///////////////////////////////////////////////////////////////////////////
  112.     
  113.     function createXmlHttpObjectIndex()
  114.     {
  115.         var i = Math.random()*100;
  116.         i = Math.round(i);
  117.         
  118.         WiseStampUtils.log("[comm.js::acquireXmlHttpObjectIndex] Created object at index " + i);
  119.             
  120.         WiseStampComm.xmlHttpObjects[i] = {};
  121.         WiseStampComm.xmlHttpObjects[i].isFree = false;
  122.         
  123.         return i;
  124.     }
  125.     
  126.     function getXmlHttpObject(index)
  127.     {
  128.         //WiseStampUtils.log("[comm.js::getXmlHttpObject] xmlHttpObjects = " + WiseStampComm.xmlHttpObjects);
  129.         //WiseStampUtils.log("[comm.js::getXmlHttpObject] index = " + index + ", xmlHttpObjects.length = " + WiseStampComm.xmlHttpObjects.length);
  130.         //WiseStampUtils.log("[comm.js::getXmlHttpObject] xmlHttpObjects["+index+"] = " + WiseStampComm.xmlHttpObjects[index]);
  131.         return WiseStampComm.xmlHttpObjects[index];    
  132.     }
  133.     
  134.     function releaseXmlHttpObject(index)
  135.     {
  136.         xmlHttpObjects[i] = null;
  137.     }
  138.     
  139.     pub.sendRequest = function(url,params,async,callback)
  140.     {
  141.         WiseStampUtils.log("comm.js :: sendRequest :: url = " + url);
  142.         
  143.         try {
  144.             if (params.password != undefined)    
  145.             {
  146.                 if (params.password.length != 32)
  147.                     params.password = WiseStampUtils.md5(params.password);
  148.             }
  149.             
  150.             var iXmlHttp = createXmlHttpObjectIndex();
  151.             getXmlHttpObject(iXmlHttp).xmlhttp = new XMLHttpRequest();
  152.             getXmlHttpObject(iXmlHttp).callback = callback;
  153.                 
  154.             getXmlHttpObject(iXmlHttp).xmlhttp.open("POST",this.HOST+url,async);
  155.             getXmlHttpObject(iXmlHttp).xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  156.             getXmlHttpObject(iXmlHttp).xmlhttp.send(serializeParams(params));
  157.             
  158.             if (async == true)
  159.             {
  160.                 getXmlHttpObject(iXmlHttp).xmlhttp.onreadystatechange = function() {WiseStampComm.onSendResponse(iXmlHttp);};
  161.             }
  162.             else
  163.             {
  164.                 return this.onSendResponse(iXmlHttp);
  165.             }
  166.         } 
  167.         catch (e)
  168.         {
  169.             WiseStampUtils.log("[comm.js::sendRequest] url = " + url + ", error = " + e);
  170.             return this.LOGIN_RESULT_SERVER_DOWN;
  171.         }
  172.         
  173.         return this.RESULT_OK;
  174.     }
  175.     
  176.     pub.onSendResponse = function(iXmlHttp)
  177.     {
  178.         var xmlhttp = getXmlHttpObject(iXmlHttp).xmlhttp;
  179.         var callback = getXmlHttpObject(iXmlHttp).callback;
  180.     
  181.         WiseStampUtils.log("[comm.js::onSendResponse] xmlhttp.readyState = " + xmlhttp.readyState + ", xmlhttp.status = " + xmlhttp.status);
  182.         
  183.         if (xmlhttp.readyState != 4)
  184.             return null;
  185.             
  186.         if (xmlhttp.status != 200)
  187.             return null;
  188.             
  189.         WiseStampUtils.log("[comm.js::onSendResponse] XML = " + xmlhttp.responseText);
  190.                 
  191.         // if "OK"
  192.         try {
  193.             var xml = null;
  194.             
  195.             if (xmlhttp.responseXML != undefined) 
  196.                 xml = xmlToObject(xmlhttp.responseXML.firstChild);
  197.             
  198.             if (xml == null || xml.nodes.result == undefined)
  199.             {
  200.                 xml = null;
  201.                 WiseStampUtils.log("<<< WiseStampComm.onSendResponse. Problem with server. XML = " + xmlhttp.responseText);
  202.             }
  203.                 
  204.             if (callback != null)
  205.             {
  206.                 //WiseStampUtils.log("[comm.js::onSendResponse] calling callback..." + callback);
  207.                 callback(xml);
  208.             }
  209.  
  210.             //releaseXmlHttpObject(iXmlHttp);
  211.                             
  212.             return xml;
  213.         }
  214.         catch (e)
  215.         {
  216.             WiseStampUtils.log("[comm.js::onSendResponse] " + e);
  217.         }
  218.  
  219.         return null;
  220.     }
  221.  
  222.     ///////////////////////////////////////////////////////////////////////////
  223.     
  224.     pub.writeLogResponse = function(xml)
  225.     {
  226.         if (xml.nodes.result == "0")
  227.             WiseStampUtils.log("WiseStampComm.writeLogResponse. result = " + xml.nodes.result);
  228.     }
  229.     
  230.     pub.writeLog = function(event,param1,param2,param3,param4,param5)
  231.     {
  232.         if (WiseStampComm.loadSettings('collect_stats') != 1)
  233.             return;
  234.         
  235.         param1 = param1 != null ? param1 : "";
  236.         param2 = param2 != null ? param2 : "";
  237.         param3 = param3 != null ? param3 : "";
  238.         param4 = param4 != null ? param4 : "";
  239.         param5 = param5 != null ? param5 : "";
  240.         var params = {event:event, user_id:WiseStampUtils.getUserCode(), param1:param1, param2:param2, param3:param3, param4:param4, param5:param5};
  241.         this.sendRequest(URL_WRITE_LOG,params,true,pub.writeLogResponse);
  242.     }
  243.     
  244.     ///////////////////////////////////////////////////////////////////////////
  245.     
  246.     pub.login = function(email,password,callback,async)
  247.     {
  248.         var params = {email:email, password:password};
  249.         return this.sendRequest(URL_LOGIN,params,async,callback);
  250.     }
  251.     
  252.     pub.register = function(email,password,firstname,lastname)
  253.     {
  254.         var params = {email:email, password:password, firstname:firstname, lastname:lastname};
  255.         var xml = this.sendRequest(URL_REGISTER,params,false);
  256.         
  257.         return xml;
  258.     }
  259.  
  260.     ///////////////////////////////////////////////////////////////////////////
  261.     
  262.     pub.saveSignatures = function(email,password,signatures)
  263.     {
  264.         var params = {email: email, password: password};
  265.         
  266.         for (var i = 0; i < signatures.length; i++)
  267.         {
  268.             params['signature_'+i+'_type'] = signatures[i].type;
  269.             params['signature_'+i+'_data'] = signatures[i].data;
  270.         }
  271.         
  272.         var xml = this.sendRequest(URL_SAVE_SIGNATURES,params,false);
  273.         return xml;
  274.     }
  275.     
  276.     pub.loadSignatures = function(email,password,type)
  277.     {
  278.         var params = {email: email, password: password};
  279.         var xml = this.sendRequest(URL_LOAD_SIGNATURES,params,false);
  280.         
  281.         return xml;
  282.     }
  283.     
  284.     pub.loadSignature = function(email,password,type)
  285.     {
  286.         var params = {email: email, password: password, type: type};
  287.         var xml = this.sendRequest(URL_LOAD_SIGNATURE,params,false);
  288.         
  289.         return xml;
  290.     }
  291.     
  292.     pub.loadSettings = function(parameter)
  293.     {
  294.         return false;
  295.         
  296.         if (this.mSettings == null)
  297.         {
  298.             WiseStampUtils.log("[comm.js::loadSettings] loading from server...");
  299.             
  300.             var params = {};
  301.             var xml = this.sendRequest(URL_LOAD_SETTINGS,params,false);
  302.             WiseStampUtils.log("[comm.js::loadSettings] received xml = " + xml);
  303.             if (xml != null)
  304.             {
  305.                 var settings = xml.nodes.data.nodes;
  306.                 for (var item in settings)
  307.                     settings[item] = settings[item].text;
  308.                 
  309.                 this.mSettings = settings;
  310.             }
  311.  
  312.             if (this.mSettings == null)
  313.             {
  314.                 WiseStampUtils.log("[comm.js::loadSettings] failed to load settings");
  315.                 this.mSettings = false; // change value to 'false' to indicate a failure
  316.             } else
  317.                 WiseStampUtils.log("[comm.js::loadSettings] settings loaded succesfully - " + this.mSettings.toSource());
  318.         }
  319.             
  320.         if (this.mSettings == false)
  321.             return false;
  322.             
  323.         return this.mSettings[parameter];
  324.     }
  325.     
  326.     
  327.     ///////////////////////////////////////////////////////////////////////////
  328.     
  329.     return pub;
  330. }();
  331.  
  332. WiseStampComm.init();